﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;
using Tanmia.Core;
using Tanmia.Windows.ItemCategoryScenario;

namespace Tanmia.Windows.ItemScenario
{
    /// <summary>
    /// Interaction logic for ItemEdit.xaml
    /// </summary>
    public partial class ItemEdit : RibbonWindow
    {
        ItemViewModel ItemVM = new ItemViewModel();

        public ItemEdit()
        {
            InitializeComponent();

            DataContext = ItemVM;
        }


        private void ShowValidationTool(string error)
        {
            vatError.Label = error;
            vatError.Visibility = Visibility.Visible;
        }

        private bool Save()
        {
            ItemVM.Entity.Validate();

            if (!ItemVM.Entity.IsValid())
            {
                ShowValidationTool(ItemVM.Error);
                return false;
            }
            else
            {
                try
                {
                    ItemVM.Save();

                    if (vatError.Visibility == Visibility.Visible)
                        vatError.Visibility = Visibility.Collapsed;

                    return true;

                }
                catch (Exception ex)
                {
                    ShowValidationTool(ex.Message);
                    return false;
                }
            }
        }


        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbItemCategory.SelectedIndex = 0;

            cmbRecord.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnSaveAndNew_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                btnClear_Click(sender, e);
        }

        private void btnSaveAndPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPerviewPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSetupPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = cmbItemCategory.SelectedItem;

            ItemVM.Entity = new Item();
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbItemCategory.SelectedItem = selectedItem;

            if (vatError.Visibility == Visibility.Visible)
                vatError.Visibility = Visibility.Collapsed;
        }

        private void btnSaveAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            ItemVM.Last();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            ItemVM.Next();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            ItemVM.Previous();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            ItemVM.First();
        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnBrowseImage_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddItemCategory_Click(object sender, RoutedEventArgs e)
        {
            WindowManager.ShowDialogEditor<ItemCategoryEdit>();

            ItemVM.UpdateCategories();
        }
    }
}
